package cn.middle.remote.user;

import cn.middle.remote.exchange.DefaultFuture;
import cn.middle.remote.exchange.SignalFuture;
import cn.middle.remote.handler.client.ClientProcessingHandler;
import cn.middle.remote.message.CallSignal;
import cn.middle.remote.message.EdgeSignal;
import cn.middle.remote.message.SubscribeSignal;
import cn.middle.remote.util.Util;

public class NettyUserClient extends UserClient {
	
	
	public NettyUserClient() {};
	
	@Override
	protected ClientProcessingHandler<?> createProcessingHandler() {
		return new NettyUserProcessHandler();
	}

	@Override
	public void setListener(UserListener listener) {
		this.listener = listener;
	}

	@Override
	public <T> SignalFuture call(int remoteClientId, String interfaceName, String msgId, T message) {
		CallSignal signal = new CallSignal();
		if(Util.isNotNull(msgId)) {
			signal.setId(msgId);
		}
		byte[] data = new byte[0];
		if(message != null) {
			data = serialization.encode(message);
		}
		signal.setInterfaceName(interfaceName);
		signal.setRemoteAddressClientId(remoteClientId);
		signal.setData(data);
		send(signal);
		DefaultFuture future = new DefaultFuture(signal.getId());
		future.setSerializable(serialization);
		return future;
	}

	@Override
	public <T> void refreshEdge(String msgId, T message) {
		EdgeSignal signal = new EdgeSignal();
		if(Util.isNotNull(msgId)) {
			signal.setId(msgId);
		}
		byte[] data = new byte[0];
		if(message != null) {
			data = serialization.encode(message);
		}
		signal.setData(data);
		send(signal);
		
	}

	@Override
	public <T> void subscribe(String msgId, T message) {
		SubscribeSignal signal = new SubscribeSignal();
		if(Util.isNotNull(msgId)) {
			signal.setId(msgId);
		}
		byte[] data = new byte[0];
		if(message != null) {
			data = serialization.encode(message);
		}
		signal.setData(data);
		send(signal);
		
	}

	

	
}
